package 类与对象.注解;

public class 练习 {
    public static void main(String[] args) {
        Person person1 = new Person("唐僧", Factory.getBoat());
        person1.demo();
        Person person = new Person("唐僧", Factory.getHorse());
        person.demo();

    }
}

interface Vehicles{
    void work();
}
class Horse implements Vehicles{
    @Override
    public void work() {
        System.out.println("horse work");
    }
}

class Boat implements Vehicles{

    @Override
    public void work() {
        System.out.println("boat work");
    }
}

class Factory{
    public static Horse getHorse(){
        Horse horse = new Horse();
        return horse;
    }

    public static Boat getBoat(){
        Boat boat = new Boat();
        return boat;
    }
}

class Person{
    private String name;
    private Vehicles vehicles;

    public Person(String name, Vehicles vehicles) {
        this.name = name;
        this.vehicles = vehicles;
    }

    public void demo(){
        if (name != null && vehicles != null){
            if(vehicles instanceof Horse){
                System.out.println(name + "坐船");
                ((Horse) vehicles).work();
            }else {
                System.out.println(name + "骑马");
                ( (Boat) vehicles).work();
            }
        }

    }
}

